package com.sunzm.droolsdemo;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author Administrator
 * @version 1.0
 * @date 2021-07-18 21:35
 */
public class ReadDrlToMySql {
    public static void main(String[] args) throws Exception {

        String ruleName = "rule1";
        int ruleStatus = 1;
        String ruleType = "1";

        String path = Thread.currentThread().getContextClassLoader().getResource("./").getPath();

        String drlFilePath = path + "rules/test.drl";
        String ruleCode = FileUtils.readFileToString(new File(drlFilePath), "UTF-8");

        Date createTime = new Date(System.currentTimeMillis());
        Date updateTime = createTime;
        Connection conn = DriverManager.getConnection("jdbc:mysql://82.156.210.70:3306/rule_db",
                "root", "ABC123abc.123");
        PreparedStatement pst = conn.prepareStatement("insert into canal_rule (rule_name,rule_code,rule_status," +
                "rule_type,create_time,modify_time) " +
                "values (?,?,?,?,?,?)");
        pst.setString(1,ruleName);
        pst.setString(2,ruleCode);
        pst.setInt(3,ruleStatus);
        pst.setString(4,ruleType);
        pst.setDate(5,createTime);
        pst.setDate(6,updateTime);

        boolean execute = pst.execute();

        System.out.println("execute: " + execute);

        pst.close();
        conn.close();

        //test1();
    }

    public static void test1() throws Exception {
        String ruleName = "rule2";

        String ruleCode = FileUtils.readFileToString(new File("dynamic_rule_engine/rules_drl/rule2.drl"), "utf-8");
        int ruleStatus = 1;
        String ruleType = "1";
        String ruleVersion = "1";
        String cntSqls = FileUtils.readFileToString(new File("dynamic_rule_engine/rules_drl/rule2_cnt.sql"), "utf-8");
        String seqSqls = FileUtils.readFileToString(new File("dynamic_rule_engine/rules_drl/rule2_seq.sql"), "utf-8");
        String ruleCreator = "sunzm";
        String ruleAuditor = "sunzm.d";
        Date createTime = new Date(System.currentTimeMillis());
        Date updateTime = createTime;
        Connection conn = DriverManager.getConnection("jdbc:mysql://82.156.210.70:3306/canal_rule",
                "root", "ABC123abc.123");
        PreparedStatement pst = conn.prepareStatement("insert into canal_rule (rule_name,rule_code,rule_status," +
                "rule_type,rule_version,cnt_sqls,seq_sqls,rule_creator,rule_auditor,create_time,update_time) " +
                "values (?,?,?,?,?,?,?,?,?,?,?)");
        pst.setString(1,ruleName);
        pst.setString(2,ruleCode);
        pst.setInt(3,ruleStatus);
        pst.setString(4,ruleType);
        pst.setString(5,ruleVersion);
        pst.setString(6,cntSqls);
        pst.setString(7,seqSqls);
        pst.setString(8,ruleCreator);
        pst.setString(9,ruleAuditor);
        pst.setDate(10,createTime);
        pst.setDate(11,updateTime);

        boolean execute = pst.execute();

        pst.close();
        conn.close();
    }
}
